package com.imooc.business;

import com.imooc.business.domain.Calculate;
import com.imooc.business.domain.Collect;
import com.imooc.business.domain.Metadata;
import com.imooc.business.domain.Rule;
import com.imooc.infra.repository.CalculateRepository;
import com.imooc.infra.repository.CollectRepository;
import com.imooc.infra.repository.MetaDataRepository;
import com.imooc.infra.repository.RuleRepository;
import com.imooc.module.collect.enums.SourceTypeEnum;
import com.imooc.module.reponse.AssetDataResponse;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Author Tim
 * @Description: 查询资产目录
 * @Version 1.0
 */
@Service
public class QueryAssetBusiness {

    private final RuleRepository ruleRepository;

    private final MetaDataRepository metaDataRepository;

    private final CalculateRepository calculateRepository;

    private final CollectRepository collectRepository;


    public QueryAssetBusiness(RuleRepository repository, MetaDataRepository metaDataRepository,
                              CalculateRepository calculateRepository, CollectRepository collectRepository) {
        this.ruleRepository = repository;
        this.metaDataRepository = metaDataRepository;
        this.calculateRepository = calculateRepository;
        this.collectRepository = collectRepository;
    }

    /**
     * 查询资产目录
     *
     * @return
     */
    public AssetDataResponse queryAsset() {
        List<Rule> rules = ruleRepository.listAll();
        AssetDataResponse response = new AssetDataResponse();
        List<AssetDataResponse.Rule> assetDatas = Lists.newArrayList();
        response.setRules(assetDatas);
        if (CollectionUtils.isEmpty(rules)) {
            return response;
        }
        for (Rule rule : rules) {
            AssetDataResponse.Rule assetData = new AssetDataResponse.Rule();
            assetData.setRuleId(String.valueOf(rule.getRuleId()));
            assetData.setBusinessName(rule.getBusinessName());
            assetData.setRuleName(rule.getRuleName());
            assetDatas.add(assetData);
        }
        return response;
    }

    /**
     * 查询资产目录 元数据
     *
     * @return
     */
    public AssetDataResponse queryAssetItem(String ruleId) {
        List<Metadata> metadatas = metaDataRepository.listByRuleId(Long.valueOf(ruleId));
        AssetDataResponse response = new AssetDataResponse();
        List<AssetDataResponse.Rule> assetDatas = Lists.newArrayList();
        response.setRules(assetDatas);
        if (CollectionUtils.isEmpty(metadatas)) {
            return response;
        }
        AssetDataResponse.Rule rule = new AssetDataResponse.Rule();
        rule.setRuleId(ruleId);
        assetDatas.add(rule);
        List<AssetDataResponse.Rule.MetaData> assetMetaDatas = Lists.newArrayList();
        rule.setMetaDatas(assetMetaDatas);
        for (Metadata metadata : metadatas) {
            AssetDataResponse.Rule.MetaData assetMetaData = new AssetDataResponse.Rule.MetaData();
            assetMetaData.setId(metadata.getId());
            assetMetaData.setCnName(metadata.getCnName());
            assetMetaData.setDataType(metadata.getDataType().getJavaStrType());
            assetMetaData.setPeriod(metadata.getPeriod());
            assetMetaData.setDescription(metadata.getDescription());
            assetMetaData.setDimension(metadata.getDimension());
            SourceTypeEnum sourceType = metadata.getSourceType();
            if (sourceType == SourceTypeEnum.CALCULATE) {
                //如果是计算类型，把详细的计算表达式返回
                Calculate calculate = calculateRepository.getByRuleIdAndEnName(metadata.getRuleId(), metadata.getEnName());
                if (Objects.nonNull(calculate)) {
                    assetMetaData.setExpression(calculate.getExpression());
                }
            }
            if (sourceType == SourceTypeEnum.COLLECT) {
                Collect collect = collectRepository.getByRuleIdAndEnName(metadata.getRuleId(), metadata.getEnName());
                if (Objects.nonNull(collect)) {
                    assetMetaData.setCollectDataSource(collect.getDataSource());
                }
            }
            assetMetaDatas.add(assetMetaData);
        }
        return response;
    }

}
